<?php
class TagLib4Jcms extends TagLib {
	
	//标签定义
	//attr 属性列表 close 是否闭合（0 或者1 默认1） alias 标签别名 level 嵌套层次
	protected $tags = array (
		'flink' => array ('attr' => 'num'),
		'slide' => array ('attr' => 'num'),
		'arclist' => array ('attr' => 'catid,orderby,num'),
		'piclist' => array ('attr' => 'catid,orderby,num'),
		'downlist' => array ('attr' => 'catid,orderby,num'),
		'prolist' => array ('attr' => 'catid,orderby,num'),
		'godlist' => array ('attr' => 'table,catid,orderby,num'),
		'catalist' => array ('attr' => 'pid,num,item',level=>3),
		'content' => array ('attr' => 'table,id')
	);
	
	//友情链接标签
	public function _flink($attr, $content) {
		$tag = $this->parseXmlAttr($attr,'flink');
		$num = CommonUtil::parseInt($tag["num"]);
		$sql = "LinkHelper::getList('','order_num DESC',{$num})";
		
		$parsestr = '<?php $_result=' . $sql . ';';
		$parsestr .= 'foreach($_result as $key=>$field):?>';
		$parsestr .= $content;
		$parsestr .= '<?php endforeach;?>';
		return $parsestr;
	}
	
	//幻灯片标签
	public function _slide($attr, $content) {
		$tag = $this->parseXmlAttr($attr,'slide');
		$num = CommonUtil::parseInt($tag["num"]);
		$sql = "SlideHelper::getList('','slide_id DESC',{$num})";
		
		$parsestr = '<?php $_result=' . $sql . ';';
		$parsestr .= 'foreach($_result as $key=>$field):?>';
		$parsestr .= $content;
		$parsestr .= '<?php endforeach;?>';
		return $parsestr;
	}
	
	//文章列表标签
	public function _arclist($attr, $content) {
		$tag = $this->parseXmlAttr($attr,'arclist');
		$num = CommonUtil::parseInt($tag["num"]);
		
		//栏目ID查询SQL
		$whereSQL = "";
		$catidArr = explode(",", $tag["catid"]);
		$i = 0;
		foreach ($catidArr as $catid){
			if($i > 0){
				$whereSQL .= "OR ";
			}
			$whereSQL .= "catalog_id={$catid} ";
			$i++;
		}
		
		$orderSQL = "";
		if($tag["orderby"] == 'hot'){
			$orderSQL = "view_num DESC";
		}elseif ($tag["orderby"] == 'pubdate'){
			$orderSQL = "timeline DESC,id DESC";
		}elseif ($tag["orderby"] == 'rand'){
			$orderSQL = "RAND()";
		}
		
		$sql = "ArticleHelper::getList('{$whereSQL}','{$orderSQL}',{$num})";
		
		$parsestr = '<?php $_result=' . $sql . ';';
		$parsestr .= 'foreach($_result as $key=>$field):';
		$parsestr .= '$field["link"]=LinkUtil::getLink($field["id"],$field["catalog_id"]);?>';
		$parsestr .= $content;
		$parsestr .= '<?php endforeach;?>';
		return $parsestr;
	}
	
	//图片列表标签
	public function _piclist($attr, $content) {
		$tag = $this->parseXmlAttr($attr,'piclist');
		$num = CommonUtil::parseInt($tag["num"]);
		
		//栏目ID查询SQL
		$whereSQL = "";
		$catidArr = explode(",", $tag["catid"]);
		$i = 0;
		foreach ($catidArr as $catid){
			if($i > 0){
				$whereSQL .= "OR ";
			}
			$whereSQL .= "catalog_id={$catid} ";
			$i++;
		}
		
		$orderSQL = "";
		if($tag["orderby"] == 'hot'){
			$orderSQL = "view_num DESC";
		}elseif ($tag["orderby"] == 'pubdate'){
			$orderSQL = "timeline DESC,id DESC";
		}elseif ($tag["orderby"] == 'rand'){
			$orderSQL = "RAND()";
		}
		$sql = "PictureHelper::getList('{$whereSQL}','{$orderSQL}',{$num})";
		
		$parsestr = '<?php $_result=' . $sql . ';';
		$parsestr .= 'foreach($_result as $key=>$field):';
		$parsestr .= '$field["link"]=LinkUtil::getLink($field["id"],$field["catalog_id"]);?>';
		$parsestr .= $content;
		$parsestr .= '<?php endforeach;?>';
		return $parsestr;
	}
	
	//下载列表标签
	public function _downlist($attr, $content) {
		$tag = $this->parseXmlAttr($attr,'downlist');
		$num = CommonUtil::parseInt($tag["num"]);
		
		//栏目ID查询SQL
		$whereSQL = "";
		$catidArr = explode(",", $tag["catid"]);
		$i = 0;
		foreach ($catidArr as $catid){
			if($i > 0){
				$whereSQL .= "OR ";
			}
			$whereSQL .= "catalog_id={$catid} ";
			$i++;
		}
		
		$orderSQL = "";
		if($tag["orderby"] == 'hot'){
			$orderSQL = "view_num DESC";
		}elseif ($tag["orderby"] == 'pubdate'){
			$orderSQL = "timeline DESC,id DESC";
		}elseif ($tag["orderby"] == 'rand'){
			$orderSQL = "RAND()";
		}
		$sql = "DownloadHelper::getList('{$whereSQL}','{$orderSQL}',{$num})";
		
		$parsestr = '<?php $_result=' . $sql . ';';
		$parsestr .= 'foreach($_result as $key=>$field):';
		$parsestr .= '$field["link"]=LinkUtil::getLink($field["id"],$field["catalog_id"]);?>';
		$parsestr .= $content;
		$parsestr .= '<?php endforeach;?>';
		return $parsestr;
	}
	
	//产品列表标签
	public function _prolist($attr, $content) {
		$tag = $this->parseXmlAttr($attr,'prolist');
		$num = CommonUtil::parseInt($tag["num"]);
		
		//栏目ID查询SQL
		$whereSQL = "";
		$catidArr = explode(",", $tag["catid"]);
		$i = 0;
		foreach ($catidArr as $catid){
			if($i > 0){
				$whereSQL .= "OR ";
			}
			$whereSQL .= "catalog_id={$catid} ";
			$i++;
		}
		
		$orderSQL = "";
		if($tag["orderby"] == 'hot'){
			$orderSQL = "view_num DESC";
		}elseif ($tag["orderby"] == 'pubdate'){
			$orderSQL = "timeline DESC,id DESC";
		}elseif ($tag["orderby"] == 'rand'){
			$orderSQL = "RAND()";
		}
		$sql = "ProductHelper::getList('{$whereSQL}','{$orderSQL}',{$num})";
		
		$parsestr = '<?php $_result=' . $sql . ';';
		$parsestr .= 'foreach($_result as $key=>$field):';
		$parsestr .= '$field["link"]=LinkUtil::getLink($field["id"],$field["catalog_id"]);?>';
		$parsestr .= $content;
		$parsestr .= '<?php endforeach;?>';
		return $parsestr;
	}
	
	//万能列表标签
	public function _godlist($attr, $content) {
		$tag = $this->parseXmlAttr($attr,'godlist');
		$table = $tag["table"];
		$num = CommonUtil::parseInt($tag["num"]);
		
		//栏目ID查询SQL
		$sql = "SELECT * FROM ".C("DB_PREFIX").$table;
		$catidArr = explode(",", $tag["catid"]);
		$i = 0;
		foreach ($catidArr as $catid){
			if($i == 0){
				$sql .= " WHERE";
			}else{
				$sql .= " OR";
			}
			$sql .= " catalog_id={$catid}";
			$i++;
		}
		if($tag["orderby"] == 'hot'){
			$sql .= " ORDER BY view_num DESC";
		}elseif ($tag["orderby"] == 'pubdate'){
			$sql .= " ORDER BY timeline DESC,id DESC";
		}elseif ($tag["orderby"] == 'rand'){
			$sql .= " ORDER BY RAND()";
		}
		if($num != 0){
			$sql .= " LIMIT {$num}";
		}
		$sqlx = "ArticleHelper::queryList('{$sql}')";
		
		$parsestr = '<?php $_result=' . $sqlx . ';';
		$parsestr .= 'foreach($_result as $key=>$field):';
		$parsestr .= '$field["link"]=LinkUtil::getLink($field["id"],$field["catalog_id"]);?>';
		$parsestr .= $content;
		$parsestr .= '<?php endforeach;?>';
		return $parsestr;
	}
	
	//栏目列表标签
	public function _catalist($attr, $content) {
		
		$tag = $this->parseXmlAttr($attr,'catalist');
		$num = CommonUtil::parseInt($tag["num"]);
		$item = empty($tag["item"])?"field":$tag["item"];
		$pid = $tag["pid"];
		
		//栏目ID查询SQL
		$sql = "";
		if(CommonUtil::contain($pid, "$")){
			$sql = "CatalogHelper::getList(\"pid=\".".$pid.",\"order_num DESC\",{$num})";
		}else{
			$sql = "CatalogHelper::getList('pid={$pid}','order_num DESC',{$num})";
		}
		
		$parsestr = '<?php $_'.$item.'result=' . $sql . ';';
		$parsestr .= 'foreach($_'.$item.'result as $'.$item.'key=>$'.$item.'):';
		$parsestr .= '$'.$item.'["first"]=$'.$item.'key==0?true:false;';
		$parsestr .= '$'.$item.'["last"]=$'.$item.'key==(count($_'.$item.'result)-1)?true:false;';
		$parsestr .= '$'.$item.'["current"]=$'.$item.'["catalog_id"]==$current_catalog["catalog_id"]?true:false;';
		$parsestr .= '$'.$item.'["link"]=LinkUtil::getCataLink($field);?>';
		$parsestr .= $content;
		$parsestr .= '<?php endforeach;?>';
		return $parsestr;
	}
	
	//单篇内容标签
	public function _content($attr, $content) {
		$tag = $this->parseXmlAttr($attr,'content');
		$table = $tag["table"];
		$id = $tag["id"];
		
		//栏目ID查询SQL
		$sql = "";
		if($table == "singel_article"){
			$sql = "SELECT * FROM ".C("DB_PREFIX").$table.".*,".C("DB_PREFIX").$table.".picture_addr LEFT JOIN ".C("DB_PREFIX")."catalog ON ".C("DB_PREFIX").$table.".catalog_id=".C("DB_PREFIX")."catalog.catalog_id WHERE id={$id}";
		}else{
			$sql = "SELECT * FROM ".C("DB_PREFIX").$table." WHERE id={$id}";
		}
		
		$sqlx = "ArticleHelper::queryObject('{$sql}')";
		
		$parsestr = '<?php $field=' . $sqlx . ';';
		$parsestr .= '$field["link"]=LinkUtil::getLink($field["id"],$field["catalog_id"]);?>';
		$parsestr .= $content;
		return $parsestr;
	}
	
	

}

?>
